diff options
| author | Fuwn <[email protected]> | 2026-01-24 13:09:50 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-01-24 13:09:50 +0000 |
| commit | 396acf3bbbe00a192cb0ea0a9ccf91b1d8d2850b (patch) | |
| tree | b9df4ca6a70db45cfffbae6fdd7252e20fb8e93c /src/app/api/websites/[websiteId]/values/route.ts | |
| download | umami-main.tar.xz umami-main.zip | |
Created from https://vercel.com/new
Diffstat (limited to 'src/app/api/websites/[websiteId]/values/route.ts')
| -rw-r--r-- | src/app/api/websites/[websiteId]/values/route.ts | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/app/api/websites/[websiteId]/values/route.ts b/src/app/api/websites/[websiteId]/values/route.ts new file mode 100644 index 0000000..172325e --- /dev/null +++ b/src/app/api/websites/[websiteId]/values/route.ts @@ -0,0 +1,50 @@ +import { z } from 'zod'; +import { EVENT_COLUMNS, FILTER_COLUMNS, SEGMENT_TYPES, SESSION_COLUMNS } from '@/lib/constants'; +import { getQueryFilters, parseRequest } from '@/lib/request'; +import { badRequest, json, unauthorized } from '@/lib/response'; +import { dateRangeParams, fieldsParam, searchParams } from '@/lib/schema'; +import { canViewWebsite } from '@/permissions'; +import { getWebsiteSegments } from '@/queries/prisma'; +import { getValues } from '@/queries/sql'; + +export async function GET( + request: Request, + { params }: { params: Promise<{ websiteId: string }> }, +) { + const schema = z.object({ + type: fieldsParam, + ...dateRangeParams, + ...searchParams, + }); + + const { auth, query, error } = await parseRequest(request, schema); + + if (error) { + return error(); + } + + const { websiteId } = await params; + + if (!(await canViewWebsite(auth, websiteId))) { + return unauthorized(); + } + + const { type } = query; + + if (!SESSION_COLUMNS.includes(type) && !EVENT_COLUMNS.includes(type) && !SEGMENT_TYPES[type]) { + return badRequest(); + } + + let values: any[]; + + if (SEGMENT_TYPES[type]) { + values = (await getWebsiteSegments(websiteId, type))?.data?.map(segment => ({ + value: segment.name, + })); + } else { + const filters = await getQueryFilters(query, websiteId); + values = await getValues(websiteId, FILTER_COLUMNS[type], filters); + } + + return json(values.filter(n => n).sort()); +} |